package entites;

import utils.LANPackage;
import utils.PackageType;
import eduni.simjava.Sim_entity;
import eduni.simjava.Sim_event;
import eduni.simjava.Sim_port;
import eduni.simjava.Sim_stat;
import eduni.simjava.Sim_system;
import eduni.simjava.distributions.Sim_negexp_obj;

public class Printer extends Sim_entity {

	private Sim_negexp_obj delayer;
	private Sim_port fromLan, toLan;
	private Sim_stat stat;

	public Printer(String name, double mean) {
		super(name);

		// Configuração da distribuição
		this.delayer = new Sim_negexp_obj(name, mean);
		this.add_generator(this.delayer);

		// Configuração das portas
		this.fromLan = new Sim_port("fromLan");
		this.add_port(fromLan);
		this.toLan = new Sim_port("toLan");
		this.add_port(toLan);

		// Configuração das medidas que serao feitas
		this.stat = new Sim_stat();
		this.stat.add_measure(Sim_stat.RESIDENCE_TIME);
		this.stat.add_measure(Sim_stat.THROUGHPUT);
		this.stat.add_measure(Sim_stat.UTILISATION);
		this.stat.add_measure(Sim_stat.WAITING_TIME);
		this.stat.add_measure(Sim_stat.SERVICE_TIME);
		this.set_stat(this.stat);
	}

	@Override
	public void body() {
		while (Sim_system.running()) {
			Sim_event e = new Sim_event();
			this.sim_get_next(e);

			if (e.get_tag() < 0) {
				this.sim_completed(e);
				break;
			}

			PackageType packageType = PackageType.values()[e.get_tag()];

			if (packageType.compareTo(PackageType.REQUEST) == 0) {
				LANPackage lanPackage = (LANPackage) e.get_data();
				this.sim_process(this.delayer.sample());

				this.sim_schedule(toLan, 0.0, PackageType.RESPONSE.ordinal(),
						lanPackage);
				this.sim_completed(e);
			}

		}
	}
}
